package junit;

import java.util.Date;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.maiyoframework.assist.entity.User;
import org.maiyoframework.assist.service.UserSvc;
import org.maiyoframework.core.jpa.Condition;
import org.maiyoframework.core.jpa.Condition.Connector;
import org.maiyoframework.core.jpa.Finder;
import org.maiyoframework.core.web.page.Page;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CommonUnitTest {

	private static UserSvc userSvc;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		try {
			ApplicationContext ac = new ClassPathXmlApplicationContext(
					"junit/spring-junit.xml");
			userSvc = (UserSvc) ac.getBean("userSvcImpl");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void save() {
		for (int i = 0; i < 1; i++) {
			User user = new User();
			user.setEmail("suner" + i + "@163.com");
			user.setIsDisabled(false);
			user.setLastLoginTime(new Date());
			user.setLoginCount(1);
			user.setPassword("22");
			user.setRegisterTime(new Date());
			userSvc.save(user);
		}
	}
	
	@Test
	public void delele(){
		// 删除多个
//		System.out.println(userSvc.delete(44));
//		Integer count = userSvc.deleteAll(48,49,44);
//		System.out.println(count);
		// 通过ID删除
		User user = userSvc.deleteById(17);
		if(user != null){
			System.out.println("已删除 ：" + user.getEmail());
		}
		// 一般用法
//		User user = userSvc.findById(7);
//		if(user != null){
//			userSvc.delete(user);
//		}
	}
	
	@Test
	public void update(){
		User user = userSvc.findById(1);
		user.setEmail("220222@qq.com");
		System.out.println(userSvc.update(user));
	}
	
	@Test
	public void finderAndConditions(){
		/**
		 * 构造Finder方式一
		 */
//		Finder finder = new Finder();
//		finder.setWhere(new Condition("email like", "%kenights%"));
		// connector 'OR' test
//		finder.setWhere(new Condition("email =", "2202@qq.com", Connector.OR));
//		finder.setWhere(new Condition("lastLoginTime is null"));
		// 查询为空方式
//		finder.setWhere(new Condition("email like '%kenights%'"));
//		finder.setWhere(new Condition("lastLoginTime is null"));
//		finder.setWhere(new Condition("lastLoginTime is null", null));
		
		/**
		 * 构造Finder方式二
		 */
		Finder finder = new Finder();
		Condition c1 = new Condition();
		c1.setProperty("email like");
		c1.setParameter("%kenights%");
		c1.setConnector(Connector.OR);
		finder.setWhere(c1);
		Condition c2 = new Condition();
		c2.setProperty("password =");
		c2.setParameter("33");
		finder.setWhere(c2);
		List<User> users = userSvc.findList(finder);
		for(User u : users){
			System.out.println(u.getId());
		}
	}
	
	@Test
	public void findUnique(){
		// 简单方式构造Finder，并设置单一条件
		Finder finder = new Finder(new Condition("email =","kenights0@163.com"));
		User user = userSvc.findUnique(finder);
		System.out.println(user.getRegisterTime());
	}
	
	@Test 
	public void findById(){
		System.out.println(userSvc.findById(1));
	}
	
	@Test
	public void findPage() {
		Page<User> page = userSvc.findPage(null, 1, 10);
		System.out.println(page.getPaging());
	}
	
	@Test
	public void findList(){
		Finder finder = new Finder();
		finder.setWhere(new Condition("isDisable =", false));
		finder.setWhere(new Condition("lastLoginTime is null"));
		finder.setOrderBy("registerTime", "asc");
		List<User> users = userSvc.findList(finder, 0, 5);
		for(User u : users){
			System.out.println(u.getEmail());
		}
	}
	
	@Test
	public void countResult(){
		Finder finder = new Finder();
		finder.setWhere(new Condition("email like","%k%"));
		finder.setWhere(new Condition("isDisabled=",false));
		System.out.println(userSvc.countResult(finder));
	}

}
